package com.mudalov.rec.dao.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

import com.mudalov.rec.dao.interfaces.IDbInitializer;

/**
 * Class <HbnDbInitializer> represents tool for initialize 
 * database content
 * 
 * @author mudalov
 *
 */
abstract class HbnDbInitializer implements IDbInitializer {
	
	private SessionFactory sessionFactory;
	
	private Configuration configuration;
	
	public HbnDbInitializer(SessionFactory sessionFactory,
			Configuration configuration) {
		this.sessionFactory = sessionFactory;
		this.configuration = configuration;
	}

	public Configuration getConfiguration() {
		return this.configuration;
	}
	
	public SessionFactory getSessionFactory() {
		return this.sessionFactory;
	}

	public void dropSchema() {
		Configuration cfg = getConfiguration();
		SchemaExport schemaExport = new SchemaExport(cfg);
		schemaExport.drop(true, true);
	}
	
	public void createSchema() {
		Configuration cfg = getConfiguration();
		SchemaExport schemaExport = new SchemaExport(cfg);
		schemaExport.create(true, true);
	}
	
	public void updateSchema() {
		Configuration cfg = getConfiguration();
		SchemaUpdate schemaUpdate = new SchemaUpdate(cfg);
		schemaUpdate.execute(true, true);
	}
	
	public void initSchema() {
		dropSchema();
		createSchema();
	}
}
